using System;
using System.Collections.Generic;
using System.Text;
using NetSpider.Retrieval;

namespace NetSpider.Core
{
    public class BatchRetrievalStrategy : ISearchStrategy
    {
        private readonly int _maxBatchSize;
        private IContentTree _fetchedContent;
        private int _currentBatchSize = 0;

        public BatchRetrievalStrategy(int maxBatchSize)
        {
            _maxBatchSize = maxBatchSize;
        }

        public bool ProcessContent(IFetchResult fetchResult, IFetchQueue fetchQueue)
        {
            if (fetchResult.Success && _currentBatchSize < _maxBatchSize)
            {
                if (fetchResult.GetRequest().IsReferred)
                {
                    IContentNode referrer = _fetchedContent.FindNode(fetchResult.GetRequest().ReferredByUri.ToString());
                    referrer.CreateChild(fetchResult);
                }
                else
                {
                    _fetchedContent = ContentTree.Create(fetchResult);
                }

                _currentBatchSize++;
            }
            else
            {
                // we need to discard or re-queue request
            }

            return _currentBatchSize < _maxBatchSize;
        }

        public IContentTree FetchedContent
        {
            get
            {
                throw new Exception();
            }
        }
    }
}
